package exp2.test4;

/**
 * Created by 春旺 on 2017/10/27.
 */
import com.sun.org.apache.xml.internal.serialize.LineSeparator;
import exp2.LinkedBinaryTree;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
public class TreeCal {
    private Stack oper = new Stack();
    private String [] arr;
    private List<String>arr2 = new ArrayList<>();
    private int result=0;
    //获取中缀表达式并转化为后缀表达式
    public void transform(String formula){
        //用空格来将式子分开
        arr = formula.split(" ");
        for(int ai=0;ai<arr.length;ai++) {
            if (isOperator(arr[ai])) {
                if (isFirstClassOperator(arr[ai])) {
                    oper.push(arr[ai]);
                } else if (isLastClassOperator(arr[ai])) {
                    if (oper.isEmpty() || !isFirstClassOperator((String) oper.peek())) {
                        oper.push(arr[ai]);
                    } else if (isFirstClassOperator((String) oper.peek())) {
                        for (int stadex = 0; stadex < oper.size(); stadex++) {
                            arr2.add((String) oper.pop());
                        }
                        oper.push(arr[ai]);
                    }
                }
            } else {
                arr2.add(arr[ai]);
            }
        }
        while (!oper.isEmpty()){
           arr2.add((String) oper.pop());
        }
    }
    private Stack<LinkedBinaryTree> nodeStack = new Stack();
    public void treeMaking(){
        //构造树
        LinkedBinaryTree node ;
        LinkedBinaryTree nodeL,nodeR,root;
        for(int i=0;i<arr2.size();i++){
            if(!isOperator(arr2.get(i))){
                node= new LinkedBinaryTree(arr2.get(i));
                nodeStack.push(node);
            }else {
                nodeL = nodeStack.pop();
                nodeR = nodeStack.pop();
                root = new LinkedBinaryTree(arr2.get(i),nodeL,nodeR);
                nodeStack.push(root);
            }
        }
        root = nodeStack.pop();
    }

    public String cal(){
        int a,b;
        Stack numSave = new Stack();
        for(int index=0;index<arr2.size();index++){
            if(!isOperator(arr2.get(index))){
                numSave.push(arr2.get(index));
            }else {
                if(arr2.get(index).equals("+")){
                    a = Integer.parseInt((String) numSave.pop());
                    b = Integer.parseInt((String) numSave.pop());
                    result = a + b;
                    numSave.push(String.valueOf(result));
                }
                if(arr2.get(index).equals("-")){
                    a = Integer.parseInt((String) numSave.pop());
                    b = Integer.parseInt((String) numSave.pop());
                    result = a - b;
                    numSave.push(String.valueOf(result));
                }
                if(arr2.get(index).equals("*")){
                    a = Integer.parseInt((String) numSave.pop());
                    b = Integer.parseInt((String) numSave.pop());
                    result = a * b;
                    numSave.push(String.valueOf(result));
                }
                if(arr2.get(index).equals("/")){
                    a = Integer.parseInt((String) numSave.pop());
                    b = Integer.parseInt((String) numSave.pop());
                    result = a / b;
                    numSave.push(String.valueOf(result));
                }
            }
        }
        result = Integer.parseInt((String) numSave.pop());
        return "结果是: "+result;
    }
    //判断是否是符号
    private boolean isOperator(String operator){
        boolean judgement = false;
        if(operator.equals("+")||operator.equals("-")||
                operator.equals("*")||operator.equals("/")){
            judgement = true;
        }
        return judgement;
    }

    // 第一类的运算符，优先级比较高
    private boolean isFirstClassOperator(String operator){
        boolean judgement = false;
        if(operator.equals("*")||operator.equals("/")){
            judgement = true;
        }
        return judgement;
    }

    // 第二类运算符，第二优先级
    private boolean isLastClassOperator(String operator){
        boolean judgement = false;
        if(operator.equals("+")||operator.equals("-")){
            judgement = true;
        }
        return judgement;
    }
}
